<?php
namespace Protocols;
class RvAscII
{
    
    public static function input($buffer)
    {
        
        if(strlen($buffer)< 14 ){
            //不到命令标识数量，返回0继续等待数据
            return 0;
        }
        $afterLen = substr($buffer,12,2);
        $len = hexdec($afterLen)*2+14;
        return $len;
    }

    /**
     * 打包，当向客户端发送数据的时候会自动调用
     * @param string $buffer
     * @return string
     */
    public static function encode($buffer)
    {
        
        /*$data = json_decode($jsonData,true);
        $result ="";
        switch ($data['type']) {
            case '1': //心跳响应
                $result= '410000006F06'.sprintf('%02X',date('y')).sprintf('%02X',date('m')).sprintf('%02X',date('d')).sprintf('%02X',date('H')).sprintf('%02X',date('i')).sprintf('%02X',date('s'));
                break;
            case '2': //下载参数
                $result = '410000000070';
                $message = $data['d'];
                $d = '';
                for($i=0;$i<32;$i++){
                  $d.=sprintf('%04X',$message['p'.($i+1)]);
                }
                $result.=sprintf('%02X',strlen($d)/2).$d;
                break;
            case '3'://上传状态
                
                # code...
                break;
            case '10': //命令请求

                break;
            case '13': //心跳响应

                break;
        }
        return $result;*/
        return $buffer;
    }

    /**
     * 解包，当接收到的数据字节数等于input返回的值（大于0的值）自动调用
     * 并传递给onMessage回调函数的$data参数
     * @param string $buffer
     * @return string
     */
    public static function decode($buffer)
    {
        
        /*$c = substr($buffer,11,2);
        $data = [];
        switch ($c) {
            case '6F': //时间同步 heart 心跳 每4秒一次 41000000006F04000DE2B0
                $len = hexdec(substr($buffer,12,2)); //长度
                $data['c'] = '1';
                $data['t'] = time();
                $data['ct'] = date('Y-m-d H:i:s');
                $data['m'] = substr($buffer,14,$len*2);//机器号字节码
                $data['i'] = $data['d'] = hexdec($data['m']);
                $data['h'] = $buffer;
                break;
            case '70': //下载参数
                $len = hexdec(substr($buffer,12,2)); //长度
                $data['c'] = '2';
                $data['t'] = time();
                $data['ct'] = date('Y-m-d H:i:s');
                $data['m'] = substr($buffer,14,$len*2);//机器号字节码
                $data['i'] = $data['d'] = hexdec($data['m']);
                $data['h'] = $buffer;
                break;
            case '71': //上传状态
                $len = hexdec(substr($buffer,12,2)); //长度
                $data['c'] = '3';
                $data['t'] = time();
                $data['ct'] = date('Y-m-d H:i:s');
                $data['m'] = substr($buffer,14,4);//机器号字节码
                $data['d'] = hexdec($data['m']);
                $data['i'] = substr($buffer,18,$len-4);
                $data['h'] = $buffer;
                # code...
                break;
            case '72': //账户验证 ID+Miami
                
                break;
            case '73': //账户验证 IC卡
                break;
            case '74': //账户验证 手机+密码
                break;
            case '75': //预结算
                break;
            case '76': //结算
                break;
            case '78': //主机发送命令
                
                break;
            default:
                $data['c'] = '0';
                $data['t'] = time();
                $data['m'] = '';//机器号字节码
                $data['d'] = '';
                $data['h'] = $buffer;
                break;
        }
        return json_encode($data);*/
        return \strtoupper($buffer);
    }
}